package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
	Sex  string `json:"sex"`
	Id   int    `json:"id"`
}

func main() {
	r := gin.Default()
	r.LoadHTMLGlob("views/*")
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Hello world!",
			"code":    200,
			"success": true,
		})
	})
	r.GET("/string", func(c *gin.Context) {
		c.String(http.StatusOK, "/string")
	})
	r.GET("/json", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{
			"message": "json",
		})
	})
	r.GET("/user", func(c *gin.Context) {
		var user = User{
			Name: "张三",
			Age:  18,
			Sex:  "男",
			Id:   1,
		}
		c.JSON(http.StatusOK, user)
	})
	r.GET("/getXml", func(ctx *gin.Context) {
		ctx.XML(http.StatusOK, gin.H{
			"message": "xml",
			"code":    200,
			"success": true,
		})
	})
	r.GET("index", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", gin.H{
			"title": "首页",
		})
	})
	r.Run(":9000")
}
